package sg.edu.nus.iss.vmcs.maintenance;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.*;

import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.*;

/**
 * This interface is a part of the Maintenance Panel. It is used by the Maintainer
 * to display the total number of cans of each Drinks Brand that are currently held
 * in the Drinks Store. This object contains buttons which represent (and to select)
 * the drinks brands served by the vending machine. This class inherits from the
 * Java AWT Panel class.
 *
 * @version 1.0  07/12/2011
 * @author Team 7S - SE19 */

public class DrinkDisplay extends Panel {

/**
 * Title for the Panel
 */	
	public final static String TITLE = "Quantity of Drinks Available";

	private StoreController storeCtrl;
	private MaintenanceController mCtrl;
	private ButtonItemDisplay bi;
	private LabelledDisplay price;
	private int curIdx; //current displayed item index;

/**
 * This constructor creates an instance of the DrinkDisplay. It receives
 * the reference for the Maintenance Controller object which is assigned
 * for further usage of the operations from maintenance controller class.
 * 
 * @param mctrl Maintenance Controller
 * @see MaintenanceController
 */	
	public DrinkDisplay(MaintenanceController mctrl) {
		mCtrl = mctrl;
		storeCtrl = mCtrl.getMainController().getStoreController();

		this.setLayout(new BorderLayout());
		int len;
		len = storeCtrl.getStoreSize(Store.DRINK);
		System.out.println("DrinkDisplay: len:" + len);
		StoreItem[] items = storeCtrl.getStoreItems(Store.DRINK);

		bi = new ButtonItemDisplay(TITLE, items, len);

		bi.addListener(new DrinkDisplayListener(mCtrl));
		bi.clear();
		price = new LabelledDisplay("Brand Price", 4, LabelledDisplay.FLOW);

		PriceDisplayListener pdl;

		pdl = new PriceDisplayListener(mCtrl);
		price.addListener(pdl);
		Panel tp = new Panel();
		tp.setLayout(new FlowLayout(FlowLayout.CENTER));
		tp.add(bi);
		curIdx = 0;
		this.add("Center", tp);
		this.add("South", (Panel)price.getLabelledDisplay());
		price.getLabelledDisplay().setEnabled(false);
	}

/**
 * This operation gets the price for Drink Brand.
 * @return price for the Drink
 */	
	public LabelledDisplay getPriceDisplay() {
		return price;
	}

/**
 * This operation activates the Drink Display, if the parameter is TRUE. Otherwise,
 * the Drink Display is deactivated
 * @param st boolean status for Drink Display Active / Deactive (TRUE / FALSE).
 */	
	public void setActive(boolean st) {
		price.setActive(st);
		bi.setActive(st);
	}

/**
 * This operation displays the stock value received for the currently selected
 * brand. The display will be done on a text field associated with the Drink
 * Display object.
 * @param idx Integer, Index of the Drink Brand in the collection.
 * @param qty Quantity of the selected drink brand
 * @throws VMCSException Index overflow
 * @see ButtonItemDisplay#clear()
 * @see ButtonItemDisplay#displayQty(int, int)
 */
	public void displayQty(int idx, int qty) throws VMCSException {
		curIdx = idx;
		bi.clear();
		price.getLabelledDisplay().setEnabled(true);
		bi.displayQty(idx, qty);
	}

/**
 * This operation retrieves the current index of the selected brand 
 * from the collection.
 * @return Integer, Current index of the selected brand
 */
	public int getCurIdx() {
		return curIdx;
	}

}